package com.turtle.security.common.utils;

import com.turtle.security.common.constant.HttpStatus;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;


/**
 * 响应信息主体
 *
 * @param <T>
 * @author turtle
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    @ApiModelProperty(value = "状态码")
    private int code;

    @Getter
    @Setter
    @ApiModelProperty(value = "返回信息")
    private String msg;


    @Getter
    @Setter
    @ApiModelProperty(value = "返回数据")
    private T data;


    public static <T> ApiResult<T> success() {
        return restResult(null, HttpStatus.SUCCESS, null);
    }

    public static <T> ApiResult<T> success(T data) {
        return restResult(data, HttpStatus.SUCCESS, null);
    }

    public static <T> ApiResult<T> success(T data, String msg) {
        return restResult(data, HttpStatus.SUCCESS, msg);
    }

    public static <T> ApiResult<T> error() {
        return restResult(null, HttpStatus.ERROR, null);
    }

    public static <T> ApiResult<T> error(String msg) {
        return restResult(null, HttpStatus.ERROR, msg);
    }

    public static <T> ApiResult<T> error(T data) {
        return restResult(data, HttpStatus.ERROR, null);
    }

    public static <T> ApiResult<T> error(T data, String msg) {
        return restResult(data, HttpStatus.ERROR, msg);
    }

    public static <T> ApiResult<T> unauthorized(int code, String msg) { return restResult(null, code, msg); }

    private static <T> ApiResult<T> restResult(T data, int code, String msg) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}

